﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace WallCreateAndEdit.WallModule
{
	public partial class BreakWallByLevelForm : System.Windows.Forms.Form
	{
		public BreakWallByLevelForm(ExternalCommandData cmdData, SortedList levels)
		{
			this.InitializeComponent();
			this.clbLevels.Items.Clear();
			for (int i = 0; i < levels.Count; i++)
			{
				Level level = levels.GetByIndex(i) as Level;
				this.clbLevels.Items.Add(level.Name);
			}
			for (int j = 0; j < this.clbLevels.Items.Count; j++)
			{
				this.clbLevels.SetItemChecked(j, true);
			}
			base.StartPosition = FormStartPosition.Manual;
			if (!this.ReadData())
			{
				base.Location = RevitVersionFuncs.GetRevitTopLeftPoint(cmdData);
			}
		}

		public List<int> GetSelectLevelIndex()
		{
			List<int> list = new List<int>();
			for (int i = 0; i < this.clbLevels.Items.Count; i++)
			{
				if (this.clbLevels.GetItemChecked(i))
				{
					list.Add(i);
				}
			}
			return list;
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void BreakWallByLevelForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			byte bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			int wMsg = 7;
			BreakWallByLevelForm.SendMessage(CmdBreakWallByLevel._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			BreakWallByLevelForm.keybd_event(bVk, 0, dwFlags, 0);
			BreakWallByLevelForm.keybd_event(bVk, 0, dwFlags2, 0);
		}

		private void BreakWallByLevelForm_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				base.Close();
			}
		}

		private void BreakWallByLevelForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.SaveData();
		}

		private void SaveData()
		{
			string tempPath = Path.GetTempPath();
			string path = tempPath + "BreakWall.dat";
			FileStream fileStream = new FileStream(path, FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(base.Location.X);
			streamWriter.WriteLine(base.Location.Y);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private bool ReadData()
		{
			string tempPath = Path.GetTempPath();
			string path = tempPath + "BreakWall.dat";
			if (!File.Exists(path))
			{
				return false;
			}
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			try
			{
				int x = Convert.ToInt32(streamReader.ReadLine());
				int y = Convert.ToInt32(streamReader.ReadLine());
                base.Location = new System.Drawing.Point(x, y);
			}
			catch
			{
				return false;
			}
			finally
			{
				streamReader.Close();
				fileStream.Close();
			}
			return true;
		}
	}
}
